/*
 * Login.java
 * Tela inicial do sistema
 *
 * Created on Aug 17, 2009, 12:21:48 AM
 */
package br.com.fieo.sgee.view;

import br.com.fieo.sgee.exception.UserNotFoundException;
import br.com.fieo.sgee.exception.ValidFieldException;
import br.com.fieo.sgee.logic.UserLogic;
import br.com.fieo.sgee.view.owner.Application;
import java.awt.Color;
import javax.swing.BorderFactory;

/**
 * Classe para criar a tela inicial do sistema
 * @author rbueno
 */
public class Login extends javax.swing.JFrame {

    /** Creates new form Login */
    public Login() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextPaneMessage = new javax.swing.JTextPane();
        jLabelTitle = new javax.swing.JLabel();
        jPanelLogin = new javax.swing.JPanel();
        jButtonEntrar = new javax.swing.JButton();
        jLabelLogin = new javax.swing.JLabel();
        jLabelSenha = new javax.swing.JLabel();
        jTextFieldLogin = new javax.swing.JTextField();
        jPasswordField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SGEE - Sistema de Gerenciamento de Energia Elétrica");
        setBackground(new java.awt.Color(255, 255, 255));
        setFont(new java.awt.Font("Times New Roman", 0, 10)); // NOI18N
        setForeground(new java.awt.Color(255, 255, 255));

        jTextPaneMessage.setEditable(false);
        jTextPaneMessage.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jTextPaneMessage.setForeground(new java.awt.Color(221, 0, 0));
        jTextPaneMessage.setAutoscrolls(false);
        jTextPaneMessage.setDisabledTextColor(new java.awt.Color(221, 0, 0));
        jTextPaneMessage.setDragEnabled(false);
        jTextPaneMessage.setEnabled(false);
        jTextPaneMessage.setMargin(new java.awt.Insets(3, 80, 3, 60));
        jTextPaneMessage.setOpaque(false);
        jTextPaneMessage.setRequestFocusEnabled(false);
        jTextPaneMessage.setSelectedTextColor(null);
        jTextPaneMessage.setSelectionColor(null);
        jTextPaneMessage.setVisible(false);

        jLabelTitle.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabelTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelTitle.setText("Sistema de Gerenciamento de Energia Elétrica");

        jPanelLogin.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonEntrar.setText("Entrar");
        jButtonEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEntrarActionPerformed(evt);
            }
        });

        jLabelLogin.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabelLogin.setText("Login");

        jLabelSenha.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabelSenha.setText("Senha");

        jTextFieldLogin.setDragEnabled(false);
        jTextFieldLogin.setMaximumSize(new java.awt.Dimension(14, 28));

        jPasswordField.setDragEnabled(false);
        jPasswordField.setMaximumSize(new java.awt.Dimension(14, 28));

        org.jdesktop.layout.GroupLayout jPanelLoginLayout = new org.jdesktop.layout.GroupLayout(jPanelLogin);
        jPanelLogin.setLayout(jPanelLoginLayout);
        jPanelLoginLayout.setHorizontalGroup(
            jPanelLoginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelLoginLayout.createSequentialGroup()
                .add(61, 61, 61)
                .add(jPanelLoginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabelLogin)
                    .add(jLabelSenha))
                .add(18, 18, 18)
                .add(jPanelLoginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jTextFieldLogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(111, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelLoginLayout.createSequentialGroup()
                .addContainerGap(298, Short.MAX_VALUE)
                .add(jButtonEntrar)
                .add(25, 25, 25))
        );
        jPanelLoginLayout.setVerticalGroup(
            jPanelLoginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelLoginLayout.createSequentialGroup()
                .addContainerGap(45, Short.MAX_VALUE)
                .add(jPanelLoginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelLogin)
                    .add(jTextFieldLogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(11, 11, 11)
                .add(jPanelLoginLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabelSenha)
                    .add(jPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jButtonEntrar)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabelTitle, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(89, 89, 89)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jTextPaneMessage)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanelLogin, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 103, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabelTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(29, 29, 29)
                .add(jPanelLogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jTextPaneMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-600)/2, (screenSize.height-400)/2, 600, 400);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Botão para efetuar o login no sistema
     * @param evt ação ao clicar no botão
     */
    private void jButtonEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEntrarActionPerformed


        try {

            this.loadDefaultsFields();
            UserLogic.getInstance().login(jTextFieldLogin.getText(), jPasswordField.getPassword());
            this.setVisible(false);
            Application app = new Application();
            app.setVisible(true);

        } catch (ValidFieldException ex) {
            
            jTextPaneMessage.setVisible(true);

            StringBuffer mensagem = new StringBuffer();
            for (String s : ex.getMessages().keySet()) {
                String value = ex.getMessages().get(s);
                mensagem.append(s.toString() + ": " + value.toString() + System.getProperty("line.separator"));
                if (s.toString().equals("login")) {
                    jTextFieldLogin.setBorder(BorderFactory.createLineBorder(Color.RED));
                }
                if (s.toString().equals("senha")) {
                    jPasswordField.setBorder(BorderFactory.createLineBorder(Color.RED));
                }
            }

            jTextPaneMessage.setText(mensagem.toString());

        } catch (UserNotFoundException ex) {
            jTextPaneMessage.setVisible(true);
            jTextPaneMessage.setText(ex.getMessage());
        }

    }//GEN-LAST:event_jButtonEntrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    /**
     * Para voltar os campos para o formato inicial
     */
    private void loadDefaultsFields() {

        jTextPaneMessage.setVisible(false);
        jPasswordField.setBorder(BorderFactory.createLineBorder(Color.black));
        jTextFieldLogin.setBorder(BorderFactory.createLineBorder(Color.black));

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEntrar;
    private javax.swing.JLabel jLabelLogin;
    private javax.swing.JLabel jLabelSenha;
    private javax.swing.JLabel jLabelTitle;
    private javax.swing.JPanel jPanelLogin;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JTextField jTextFieldLogin;
    private javax.swing.JTextPane jTextPaneMessage;
    // End of variables declaration//GEN-END:variables
}
